<html>
  <head>
    <title>My first PHP website</title>
  </head>
  <body>
    <h2>Registration Page</h2>
    <a href="index.php">Click here to go back</a><br/><br/>
    <form action="register.php" method="post">
      Enter Email: <input type="text" name="username" required="required"/> <br/>
      Enter Nombre: <input type="text" name="name" /> <br/>
      Enter Apellido: <input type="text" name="lastName" /> <br/>
      Enter DNI: <input type="text" name="id" /> <br/>
      Enter Password: <input type="password" name="password" required="required" /> <br/>
      Enter Repeat Password: <input type="password" name="re-password"  /> <br/>
      <input type="submit" value="Register"/>
    </form>
  </body>
</html>

<?php
if($_SERVER["REQUEST_METHOD"] == "POST"){
  $username = mysql_real_escape_string($_POST['username']);
  $password = mysql_real_escape_string($_POST['password']);
  $name = mysql_real_escape_string($_POST['name']);
  $lastName = mysql_real_escape_string($_POST['lastName']);
  $id = intval(mysql_real_escape_string($_POST['id']));
  $repeatPassword = mysql_real_escape_string($_POST['re-password']);

  $bool = true;




  mysql_connect("localhost", "root","admin") or die(mysql_error()); //Connect to server
  mysql_select_db("InfMedicas") or die("Cannot connect to database"); //Connect to database
  $query = mysql_query("Select * from users"); //Query the users table

  echo $query;

  while($row = mysql_fetch_array($query)) //display all rows from query
  {
    $table_users = $row['username']; // the first username row is passed on to $table_users, and so on until the query is finished
    if($username == $table_users) // checks if there are any matching fields
    {
      $bool = false; // sets bool to false
      Print '<script>alert("Username has been taken!");</script>'; //Prompts the user
      Print '<script>window.location.assign("register.php");</script>'; // redirects to register.php
    }
  }

  if($bool) // checks if bool is true
  {
    mysql_query("INSERT INTO users (username, name, lastName, id, password) VALUES ('$username','$name','$lastName','$id','$password')"); //Inserts the value to table users
    Print '<script>alert("Successfully Registered!");</script>'; // Prompts the user
    Print '<script>window.location.assign("register.php");</script>'; // redirects to register.php
  }

}
?>